#!/usr/bin/env bash

# LibreNMS agent to read HDD/SDD temperature using hddtemp
#
# Copyright (c) 2016 Exa-Omicron <http://exa.nl>
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.  Please see LICENSE.txt at the top level of
# the source code distribution for details.
#
# requires which, find, awk and sed
#
# optionally, install gnu parallel for a significant performance boost 
# on machines with large numbers of drives.

# Try to use lsblk if available. Otherwise, use find.
if type lsblk >/dev/null 2>&1; then
  disks=`lsblk -dnp|cut -d' ' -f1 | tr '\n' ' '`
else
  disks=`find /dev -name '[sh]d[a-z]' -or -name '[sh]d[a-z][a-z]' | tr '\n' ' '`
fi

hddtemp=`which hddtemp 2>/dev/null`

if [ "${hddtemp}" != "" ]; then
	if [ -x "${hddtemp}" ]; then
		if type parallel > /dev/null 2>&1; then
			# When available, use GNU parallel for a significant performance boost. hddtemp runs serially(!)
			output=`parallel ${hddtemp} -w -q ::: ${disks} 2>/dev/null`
		else
			output=`${hddtemp} -w -q ${disks} 2>/dev/null`
		fi
		content=`echo "$output" | awk '{ if ($0 !~ /not available/) { print $0 } }' | awk -F": " 'BEGIN{ ORS="" }{ print "|"$1"|"$2"|"$3"|";} ' | sed 's/[° ]C|/|C|/g' | sed 's/[° ]F|/|F|/g' | tr -cd '\12\14\40-\176'`
		if [ "${content}" != "" ]; then
			echo '<<<hddtemp>>>'
			echo ${content}
			echo
		else
			echo "no hddtemp compatible disks found" >&2
		fi
	else
		echo "hddtemp not executable" >&2
	fi
else
	echo "hddtemp not installed" >&2
fi
